<?php

namespace Home\Controller;

class WishController extends BaseMemberController {

    function _initialize() {
//        parent::_initialize();
        $this->_Member_mod = M("Member");
//        $this-> user_id = session('visitor.user_id');
    }

    function index() {
        $res = M('wish')->join('left join ecm_member on ecm_wish.user_id = ecm_member.user_id')
                        ->join('left join ecm_goods on ecm_wish.goods_id = ecm_goods.goods_id')
                        ->where(array('ecm_wish.user_id' => $_POST['user_id']))
                        ->select();
        $res_textarea = M('textarea')->find();

        render('10000','获取成功',array('wish' => $res,'textarea'=>$res_textarea['wish']));
    }

    function drop() {
        if(!$_POST['user_id'] || !$_POST['goods_id']){
            render('10000','参数错误');
        }
        $cond['user_id'] = $_POST['user_id'];
        $cond['goods_id'] = $_POST['goods_id'];
        $res = M('wish')->where($cond)->delete();
        if(!$res){
            render('10003','删除失败');
        }
        render('10000','删除成功',array('wish' => $res));
    }

    function add_wish()
    {
        if(!$_POST['goods_id'] || !$_POST['wish_price']){
            render('10004','参数错误');
        }
        $data['goods_id'] = $_POST['goods_id'];
        $data['user_id'] = $_POST['user_id'];
        $data['add_time'] = date("Y-m-d H:i:s");
        $data['wish_price'] = $_POST['wish_price'];
        $res = M('wish')->add($data);
        if (!$res) {
            render('10003', '添加失败');
        }
        render('10000', '加入心愿单成功', $res);
    }

    function delete_all() {
        if(!$_POST['goods_id']){
            render('10004','参数错误');
        }
        $cond['goods_id'] = $_POST['goods_id'];
        $cond['user_id'] = $_POST['user_id'];
        $condc['item_id'] = $_POST['goods_id'];
        $condc['user_id'] = $_POST['user_id'];
        $res = M('wish')->where($cond)->delete();
        $resc = M('collect')->where($condc)->delete();
        render('10000','取消成功',$res);
    }

}